Coin Toss Simulator code
#include <iostream>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
using namespace std;
int main() {
// Seed the random number generator with current time
srand(static_cast<unsigned int>(time(0)));
char choice;
do {
// Generate a random number: 0 or 1
int toss = rand() % 2;
if (toss == 0)
cout << "Result: Heads" << endl;
else
cout << "Result: Tails" << endl;
// Ask user if they want to toss again
cout << "Do you want to toss again? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Thanks for playing!" << endl;
return 0;
}
Code output
Result: Heads
Do you want to toss again? (y/n): y
Result: Tails
Do you want to toss again? (y/n): y
Result: Heads
Do you want to toss again? (y/n): n
Thanks for playing!